package com.hanson.data_conveying;

import java.util.function.Consumer;
import java.util.function.Function;
import java.util.function.Supplier;

/**
 * @description: 异步数据传输任务创建模板
 * @classname: ConveyingTask
 * @author: HansonLee
 * @create: 2024/03/26
 **/
public class ConveyingTask<T,R> implements Runnable {

    private Supplier<T> supplier;
    private Function<T,R> function;
    private Consumer<R> consumer;

    public ConveyingTask() {
    }

    public ConveyingTask<T,R> supplier(Supplier<T> supplier) {
        this.supplier = supplier;
        return this;
    }

    public ConveyingTask<T,R> function(Function<T, R> function) {
        this.function = function;
        return this;
    }

    public ConveyingTask<T,R> consumer(Consumer<R> consumer) {
        this.consumer = consumer;
        return this;
    }

    //获取并处理调整数据传输格式进行传输，记录日志
    private <T,R> void handleData2Convey(Supplier<T> supplier, Function<T,R> function, Consumer<R> consumer){
        //获取数据
        T data = supplier.get();
        //处理并传输,返回传输处理结果
        R hrs = function.apply(data);
        //记录日志
        consumer.accept(hrs);
    }



    @Override
    public void run() {
        handleData2Convey(supplier,function,consumer);
    }
}
